import typescript from '@rollup/plugin-typescript';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import clear from 'rollup-plugin-clear';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';

export function createBaseConfig(pkg) {
  const config = [
    {
      input: 'src/index.ts',
      output: [
        {
          file: pkg.main,
          format: 'cjs',
          sourcemap: false,
        },
        {
          file: pkg.module,
          format: 'esm',
          sourcemap: false,
        },
      ],
      plugins: [
        clear({
          targets: ['dist']
        }),
        peerDepsExternal(),
        resolve(),
        commonjs(),
        typescript({
          tsconfig: '../../tsconfig.json',
          declaration: true,
          declarationDir: 'dist',
        }),
        babel({
          babelHelpers: 'bundled',
          exclude: 'node_modules/**',
          extensions: ['.ts', '.tsx', '.jsx'],
        }),
        terser(),
      ],
      external: []
    },
  ];

  return config;
};